# -*- coding: UTF-8 -*-
# __author__ = 'Sengo'
from service.image_service import *
from service.ecc_service import ECC


def start():
    pic1 = "00056v"
    pic2 = "10131v"
    pic3 = "00458u"  # big file
    pic4 = "01007a"
    img1 = read_photo('01522v')
    cv.imshow("original", img1)
    # 拆分
    b1, g1, r1 = split_3_img(img1)
    # 缩小
    b, i = pyramid(b1)
    g, i = pyramid(g1)
    r, i = pyramid(r1)
    # 合并
    offset = cv.merge((b, g, r))
    cv.imshow("offset", offset)

    # ECC alingned small
    im2 = ECC(b, g)
    im3 = ECC(b, r)
    ecc_small = cv.merge((b, im2, im3))
    cv.imshow("ECC", ecc_small)

    # ECC aligned big
    im2 = ECC(b, g, b1, g1, i)
    im3 = ECC(b, r, b1, r1, i)
    ecc_big = cv.merge((b1, im2, im3))
    save_photo(ecc_big, "ecc")

    # canny img small
    canny = candy_img(ecc_small)
    cv.imshow("canny", canny)
    # rgb2hsv
    # hsv = cv.cvtColor(ecc, cv.COLOR_RGB2HSV)
    # cv.imshow("hsv", hsv)
    #
    # ret, threshold = cv.threshold(hsv, 10, 255, cv.THRESH_BINARY_INV)
    # cv.imshow("threshold", threshold)
    top, bottom, left, right = spilit_top_bottom_left_right(canny)

    cv.imshow("top", top)
    cv.imshow("bottom", bottom)
    cv.imshow("left", left)
    cv.imshow("right", right)

    # show whole pic lines
    line_threshold = 150
    lines = line_detect_whole_pic(canny, line_threshold)
    ecc_lines = ecc_big.copy()
    for line in lines:
        for x1, y1, x2, y2 in line:
            cv.line(ecc_lines, (x1, y1), (x2, y2), (255, 0, 255), 2)
    cv.imshow("whole-lines", ecc_lines)

    t, b, l, r = lines_detect(canny, line_threshold)
    if i:
        t = t * 2** i
        b = b * 2** i
        l = l * 2** i
        r = r * 2** i
    cropping = ecc_big[t:b, l:r]
    cv.imshow("cropping", cropping)
    save_photo(cropping, "cropping")



if __name__ == '__main__':
    start()
    cv.waitKey(0)